<?php
protectScript('DevNotesModel.php');

class DevNotesModel{
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'note',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdNote' => 0,
		'iDateNote' => 0,
		'sTitleNote' => '',
		'sTextNote' => ''
	);

	public function __construct($asmNoteInfo = null){
		$this->_asmWritableProps['iIdNote'] = (isset($asmNoteInfo['idNote'])) ? $asmNoteInfo['idNote'] : $this->_asmWritableProps['iIdNote'];
		$this->_asmWritableProps['iDateNote'] = (isset($asmNoteInfo['dateNote'])) ? $asmNoteInfo['dateNote'] : $this->_asmWritableProps['iDateNote'];
		$this->_asmWritableProps['sTitleNote'] = (isset($asmNoteInfo['titleNote'])) ? $asmNoteInfo['titleNote'] : $this->_asmWritableProps['sTitleNote'];
		$this->_asmWritableProps['sTextNote'] = (isset($asmNoteInfo['textNote'])) ? $asmNoteInfo['textNote'] : $this->_asmWritableProps['sTextNote'];
	}

	public function getNotes($sWhereClause = null, $sLimitClause = null){
		$oMysqliInstance = AYSMysqli::getInstance();

		if(isset($sLimitClause)){
			$sLimit = $sLimitClause;
		}else{
			$sLimit = '';
		}
		if(isset($sWhereClause)){
			$sWhere = $sWhereClause;
		}else{
			$sWhere = '';
		}
		$query = sprintf(
			'SELECT idNote, dateNote, titleNote, textNote ' .
			"FROM %s $sWhere ORDER BY dateNote DESC $sLimit",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName']
		);
		$rQueryResult = $oMysqliInstance->query($query);

		$aiasmNotesList = array();
		while($r = $rQueryResult->fetch_assoc()){
			$aiasmNotesList[] = $r;
		}
		return $aiasmNotesList;
	}

	public static function addNote($assNoteInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"INSERT INTO %s (titleNote, textNote)".
			"VALUES ('%s', '%s')",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assNoteInfos['titleNote']),
			$oMysqliInstance->escape_string($assNoteInfos['textNote'])
		);
		$oMysqliInstance->query($query);
	}

	public function editNote($assNoteInfos){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"UPDATE %s ".
			"SET titleNote = '%s', textNote = '%s' ".
			"WHERE idNote = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$oMysqliInstance->escape_string($assNoteInfos['titleNote']),
			$oMysqliInstance->escape_string($assNoteInfos['textNote']),
			$assNoteInfos['idNote']
		);

		$oMysqliInstance->query($query);
	}

	public static function delNote($iIdNote){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"DELETE FROM %s ".
			"WHERE idNote = %d",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$iIdNote
		);
		$oMysqliInstance->query($query);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'DevNotestModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
